<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
</head>
<style>
    html,
    body {
        background-color: #fbf9f9;
    }

    .placeholder_div {
        background-color: #fbf9f9;
    }

    .user_info {
        padding: 0 0.8rem;
        border-bottom: 1px solid #e8e8e8;
        height: 5rem;
    }

    .user_info_left {
        margin-top: 1.75rem;
        width: 2.3rem;
        height: 2.3rem;
        border-radius: 2.3rem;
    }

    .user_info_right {
        height: 5rem;
        width: 13.5rem;
        float: right;
    }

    .user_name {
        padding-top: 0.3rem;
        line-height: 1.3rem;
        height: 1.3rem;
        border-bottom: 1px solid #e8e8e8;
        font-size: 0.7rem;
        color: #565656;
    }

    .user_name span {
        float: right;
        font-size: 0.6rem;
        color: #838383;
    }

    .arrow_right {
        width: 0.4rem;
        height: 0.8rem;
        float: right;
        margin-top: -1.75rem;
    }

    .user_contact_item {
        padding-top: 0.5rem;
        line-height: 1rem;
    }

    .user_contact_item img {
        width: 1rem;
        height: 0.9rem;
        margin-right: 0.5rem;
        float: left;
    }

    .user_contact_item p {
        color: #838383;
        font-size: 0.6rem;
    }

    .add_btn {
        width: 11.75rem;
        height: 2.2rem;
        background-color: #2E8EFF;
        color: #FFFFFF;
        font-size: 0.8rem;
        text-align: center;
        line-height: 2.2rem;
        margin: 2.5rem 0 1rem 3.5rem;
        border-radius: 0.5rem;
    }

    .all_bingli {
        padding: 0 0.8rem;
    }

    .all_bingli .title {
        font-size: 0.7rem;
        color: #303030;
        padding: 1rem 0;
    }

    .all_bingli .bingli_item {
        width: 100%;
        /* height: 13.3rem; */
        box-shadow: 0px 0.1rem 0.7rem 0px rgba(0, 0, 0, 0.07);
        border-radius: 1rem;
        margin-bottom: 1rem;

    }

    .all_bingli .bingli_item .padding {
        padding: 1rem;
    }

    .all_bingli .bingli_item .padding .bingli_item_title {
        font-size: 0.7rem;
        color: #2E8EFF;
        border-bottom: 1px solid #e8e8e8;
        padding-bottom: 1rem;
    }

    .all_bingli .bingli_item .padding .kaifang_detail p:first-child {
        margin-top: 0.5rem;
    }

    .all_bingli .bingli_item .padding .kaifang_detail p {
        margin-top: 0.25rem;
    }

    .all_bingli .bingli_item .padding .kaifang_detail .tou {
        color: #838383;
        font-size: 0.7rem;
        display: inline-block;
        width: 4rem;
        text-align: center;
        float: left;
        margin-top: 0.1rem;
    }

    .all_bingli .bingli_item .padding .kaifang_detail .wei {
        color: #565656;
        font-size: 0.6rem;
        display: inline-block;
        width: 11rem;
        /* background-color: skyblue; */
    }

    .all_bingli .bingli_item .padding .kaifang_detail .wei span {
        margin-right: 0.8rem;
    }

    .all_bingli .bingli_item .padding .bingli_item_title span {
        float: right;
        padding: 0.1rem;
        font-size: 0.6rem;
        border: 1px solid #2E8EFF;
        border-radius: 0.2rem;
        margin-left: 0.5rem;
    }



    /*  聊天框内所有元素必须浮动起来 */
    .triangle_left {
        float: left;
        margin: 0.5rem 0 0 -0.5rem;
        width: 0;
        height: 0;
        border-top: 0.5rem solid transparent;
        border-right: 0.5rem solid #ce5743;
        border-bottom: 0.5rem solid transparent;
    }

    .triangle_right {
        float: right;
        margin: 0.5rem -0.5rem 0 0;
        width: 0;
        height: 0;
        border-top: 0.5rem solid transparent;
        border-left: 0.5rem solid skyblue;
        border-bottom: 0.5rem solid transparent;
    }

    .my {
        width: 100%;
        float: right;
        margin-top: 0.8rem;
    }

    .my .user_avater {
        width: 2rem;
        height: 2rem;
        float: right;
        margin: 0 0.5rem 0 1rem;
        border-radius: 50%;
    }

    .my .text_padding {
        float: right;
        width: 70%;
        font-size: 0.7rem;
        color: #fff;
    }

    .my .text_padding .text {
        float: right;
        background-color: skyblue;
        padding: 0.5rem 0.5rem;
        border-radius: 0.3rem;
    }

    .my .text_padding .text .my_img {
        object-fit: scale-down;
        height: 50px;
    }


    .your {
        width: 100%;
        float: left;
        margin-top: 0.8rem;
    }

    .your .user_avater {
        width: 2rem;
        height: 2rem;
        float: left;
        margin: 0 1rem 0 0.5rem;
        border-radius: 50%;
    }

    .your .text_padding {
        float: left;
        width: 70%;
        font-size: 0.7rem;
        color: #fff;
    }

    .your .text_padding .text {
        float: left;
        background-color: #ce5743;
        padding: 0.5rem 0.5rem;
        border-radius: 0.3rem;
    }

    .your .text_padding .text .your_img {
        object-fit: scale-down;
        height: 50px;
    }

    .time {
        text-align: center;
        float: left;
        width: 100%;
        padding-top: 0.8rem;
        font-size: 0.7rem;
    }

    .kaifang_case {
        background-color: #ffffff;
        width: 90%;
        margin: 0.5rem 0 0 10%;
        /* height: 5rem; */
    }

    .kaifang_case .title {
        height: 1.5rem;
        line-height: 1.5rem;
        font-size: 0.7rem;
        background-color: #d6523a;
        color: #fff;
        font-weight: bolder;
        padding: 0.2rem 0;
    }

    .kaifang_case .title img {
        height: 1rem;
        object-fit: scale-down;
        margin: 0.1rem 0.5rem;
    }

    .kaifang_case .case_detail {
        padding: 0 0.5rem;
        font-size: 0.6rem;
        color: #000000;
    }

    .kaifang_case .case_detail .case_detail_title {
        text-align: center;
        font-size: 0.7rem;
        height: 1.5rem;
        line-height: 1.5rem;
    }

    .kaifang_case .case_detail .case_detail_item {
        width: 100%;
        height: 1.2rem;
        line-height: 1.2rem;
    }

    .kaifang_case .case_detail .case_detail_item .tou {
        width: 2rem;
        text-align: center;
        display: inline-block;
    }

    .kaifang_case .case_detail .case_detail_item .wei {
        width: 4.5rem;
        display: inline-block;
    }

    .kaifang_case .bottom_div {
        height: 1.5rem;
        line-height: 1.5rem;
        font-size: 0.7rem;
        padding: 0 0.5rem 0.5rem 0.5rem;
        color: #acacac;
    }

    .kaifang_case .bottom_div img {
        float: right;
        width: 0.3rem;
        height: 0.4rem;
        margin: 0.46rem 0;
    }
</style>

<body>
    <div id="app" v-cloak>
        <div class="placeholder_div"></div>
        <!-- ------------------------------病例的HTML------------------------------- -->
        <div id="main1">

            <div class="user_info">
                <img class="user_info_left" v-if="user_avatar" :src="user_avatar" alt="">
                <img class="user_info_left" v-else src="../image/user_avater.png" alt="">

                <div class="user_info_right">
                    <p class="user_name">{{user_info.realname}}
                        <!-- <span>添加备注</span> -->
                    </p>
                    <p class="user_contact">

                    <div class="user_contact_item">
                        <img src="../image/icon_shouji.png" style="width: 0.7rem;height: 1rem;padding: 0 0.15rem;"
                            alt="">
                        <p>{{user_info.mobile}}</p>
                    </div>

                    <!-- <img src="../image/arrow_right.png" alt="" class="arrow_right"> -->
                    </p>
                </div>
            </div>
            <!-- <div class="add_btn openwin" data-win="new_buchong_bingli_win">补充病例</div> -->
            <div class="placeholder_div"></div>

            <div class="all_bingli">
                <p class="title">所有病例</p>

                <div class="bingli_item openwin" :data-id="item.id" v-for="item in bingli_list" data-win="fangan_win">
                    <div class="padding">
                        <p class="bingli_item_title">方案
                            <span>{{item.dosageForm}}</span>
                            <span v-if="item.billingMoney == 0">方案免费</span>
                            <span v-if="item.status == 0">未购药</span>
                            <span v-if="item.status == 1">已购药</span>
                        </p>

                        <div class="kaifang_detail">
                            <p class="kaifang_detail_item">
                                <span class="tou">患 者：</span><span class="wei"><span>{{item.name}}</span> <span
                                        v-if="item.sex == 1">男</span> <span v-if="item.sex == 2">女</span>
                                    <span>{{item.age}}岁</span></span>
                            </p>
                            <p class="kaifang_detail_item">
                                <span class="tou">诊 断：</span><span class="wei">{{item.dialectical}}</span>
                            </p>
                            <p class="kaifang_detail_item">
                                <span class="tou">处 方：</span><span class="wei">{{item.prescription}}</span>
                            </p>
                            <p class="kaifang_detail_item">
                                <span class="tou">诊 金：</span><span class="wei">{{item.billingMoney}}</span>
                            </p>
                            <p class="kaifang_detail_item">
                                <span class="tou">帖 数：</span><span class="wei">{{item.usageNum}}帖</span>
                            </p>
                            <p class="kaifang_detail_item">
                                <span class="tou">开方时间：</span><span class="wei">{{item.createTimeText}}</span>
                            </p>
                        </div>

                    </div>
                </div>

            </div>
        </div>
        <!-- ------------------------------咨询的HTML------------------------------- -->
        <div id="main2" style="padding-bottom: 5rem;">
            <div class="main">
                <!-- 聊天框内所有元素必须浮动起来 -->
                <div v-for="item in chat_records" v-if="item">
                    <p class="time" v-if="item.timestamp">{{item.timestamp}}</p>
                    <div :class="[item.sender == identifier ? 'my' : 'your']">
                        <img v-if="item.sender == identifier"
                            :src="doctor_avatar ? doctor_avatar  : '../image/user_avater.png'" class="user_avater"
                            alt="">
                        <img v-if="item.sender != identifier"
                            :src="user_avatar ? user_avatar : '../image/user_avater.png'" class="user_avater" alt="">

                        <div class="text_padding">
                            <div :class="[item.sender == identifier ? 'triangle_right' : 'triangle_left']"
                                v-if="item.msgType != 'json'"></div>
                            <p class="text" v-if="item.msgType == 'text'">
                                <!-- 文字类型消息 -->
                                <span v-if="item.msgType == 'text'">{{item.text}}</span>
                            </p>
                            <p class="text" v-if="item.msgType == 'image'">
                                <!-- 图片类型消息 -->
                                <img :class="[item.sender == identifier ? 'my_img' : 'your_img']"
                                    v-if="item.msgType == 'image'" :src="item.thumb" alt=""
                                    @click="viewImgs(item.largeImage)">
                            </p>
                            <!-- 方案开始 -->
                            <div class="kaifang_case" v-if="item.msgType == 'json'">
                                <div class="title">
                                    <img src="../image/icon_case.png" alt="">
                                    方案
                                </div>
                                <div class="case_detail">
                                    <p class="case_detail_title">医生给您制定了新的方案</p>
                                    <div class="case_detail_item p_hide">
                                        <span class="tou">患者：</span>
                                        <span class="wei">{{item.text.name}} <span v-if="item.text.sex == 1">男</span>
                                            <span v-if="item.text.sex == 2">女</span> {{item.text.age}}岁 </span>
                                    </div>
                                    <div class="case_detail_item p_hide">
                                        <span class="tou">病名：</span>
                                        <span class="wei">{{item.text.diseases}}</span>
                                    </div>
                                    <div class="case_detail_item p_hide">
                                        <span class="tou">处方：</span>
                                        <span class="wei">{{item.text.prescription}}</span>
                                    </div>
                                    <div class="case_detail_item p_hide">
                                        <span class="tou">诊费：</span>
                                        <span class="wei">{{item.text.billingMoney}}</span>
                                    </div>
                                </div>
                                <div class="bottom_div" @click="clickPlan(item.text.id)">
                                    点击查看详情
                                    <img src="../image/arrow_right.png" alt="">
                                </div>
                            </div>
                            <!--方案结束-->
                        </div>
                    </div>
                </div>

                <!-- 占位盒子 -->
                <div id="maodian_div" style="height: 3.5rem;width: 100%;float: left;"></div>

            </div>

        </div>
    </div>
</body>

</html>
<script src="../script/set_root.js"></script>

<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/zepto.min.js"></script>
<script src="../script/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="../script/hb.js"></script>
<script type="text/javascript" src="../script/vue.js"></script>
<script type="text/javascript">
    apiready = function () {
        hb_ready();
        // alert(JSON.stringify(api.pageParam))
        get_ajax("user/view", {
            userId: api.pageParam.uid,
        }, function (res, err) {
            if (res.code == 200) {
                app.user_avatar = IMAGE_HTTP + res.data.avatar
                app.user_info = res.data
            }
        })

        get_ajax("prescribe/plan/list-user-page", {
            userId: api.pageParam.uid,
            page: app.page,
            pagesize: 10,
        }, function (res, err) {
            if (res.code == 200) {
                // alert(JSON.stringify(res))
                app.bingli_list = res.data.data
            } else {
                // alert(JSON.stringify(err))
            }
        })

        app.side_identifier = api.pageParam.uid
        app.identifier = $api.getStorage("emp").userId

        // 即时聊天IM模块
        var tencentIm = api.require('tencentIm');

        // alert(JSON.stringify(ret));
        // 获取用户账号对应的验签
        get_ajax("im/getUserSig", {
            identifier: app.identifier
        }, function (res, err) {

            if (res.code == 200) {
                // alert(JSON.stringify(res))
                app.identifier_UserSig = res.data
                tencentIm.init({appid:'1400407726'}, function (ret, err) {
                    if (ret) {
                        // alert(JSON.stringify(ret));
                        tencentIm.login({
                            identifier: app.identifier,
                            userSig: app.identifier_UserSig,
                        }, function (ret, err) {
                            if (ret) {
                                // alert(JSON.stringify(ret));
                                tencentIm.getConversation({
                                    peer: app.side_identifier,
                                    type: 1,
                                }, function (ret, err) {
                                    if (ret) {
                                        // alert(JSON.stringify(ret));
                                        get_message()

                                    }

                                    // 新消息监听
                                    get_new_msg()
                                });
                            } else {
                                toast("登录腾讯IM失败")
                            }
                        });

                    }else {
                        alert("初始化失败");
                    }
                });
            }
        })

        $("#main2").hide()

        var UIChatBox = api.require('UIChatBox');
        api.addEventListener({
            name: 'show_chat'
        }, function (ret, err) {
            if (ret.value.index == 0) {
                $("#main1").show()
                $("#main2").hide()
                UIChatBox.hide();
            } else {
                $("#main1").hide()
                $("#main2").show()
                $('html, body').animate({
                    scrollTop: $('#maodian_div').offset().top
                }, 300)


                UIChatBox.open({
                    placeholder: '',
                    maxRows: 4,
                    emotionPath: 'widget://res/img/emotion',
                    texts: {
                        recordBtn: {
                            normalTitle: '按住说话',
                            activeTitle: '松开结束'
                        },
                        sendBtn: {
                            title: '发送'
                        }
                    },
                    styles: {
                        inputBar: {
                            borderColor: '#d9d9d9',
                            bgColor: '#f2f2f2'
                        },
                        inputBox: {
                            borderColor: '#B3B3B3',
                            bgColor: '#FFFFFF'
                        },
                        emotionBtn: {
                            normalImg: 'widget://res/img/chatBox_face1.png'
                        },
                        extrasBtn: {
                            normalImg: 'widget://image/icon_chat_more.png'
                        },
                        keyboardBtn: {
                            normalImg: 'widget://res/img/chatBox_key1.png'
                        },
                        speechBtn: {
                            normalImg: 'widget://image/icon_chat_yuyin.png'
                        },
                        recordBtn: {
                            normalBg: '#c4c4c4',
                            activeBg: '#999999',
                            color: '#000',
                            size: 14
                        },
                        indicator: {
                            target: 'both',
                            color: '#c4c4c4',
                            activeColor: '#9e9e9e'
                        },
                        sendBtn: {
                            titleColor: '#fff',
                            bg: '#66CD00',
                            activeBg: '#fff',
                            titleSize: 14
                        }
                    },
                    extras: {
                        titleSize: 12,
                        titleColor: '#565656',
                        btns: [{
                            title: '拍照',
                            normalImg: 'widget://image/icon_chat_camera.png',
                            activeImg: 'widget://image/icon_chat_camera.png'
                        }, {
                            title: '图片',
                            normalImg: 'widget://image/icon_chat_photo.png',
                            activeImg: 'widget://image/icon_chat_photo.png'
                        }, {
                            title: '开方',
                            normalImg: 'widget://image/icon_kaifang.png',
                            activeImg: 'widget://image/icon_kaifang.png'
                        }]
                    }
                }, function (ret, err) {
                    // 点击了发送
                    if (ret.eventType == "send") {
                        // alert(app.identifier)
                        // alert(app.side_identifier)
                        tencentIm.sendText({
                            peer: app.side_identifier,
                            type: 1,
                            text: ret.msg,
                        }, function (res, err) {
                            if (res) {
                                // alert(JSON.stringify(res));
                                app.chat_records.length = 0
                                get_message()
                            }
                        });
                        // post_ajax("im/send-message", {
                        //     fromId: app.identifier,
                        //     toId: app.side_identifier,
                        //     content: ret.msg,
                        //     msgType: 1,
                        // }, function (result, error) {
                        //     if (result.code == 200) {
                        //         // alert(JSON.stringify(ret))

                        //     }
                        // })


                    } else if (ret.eventType == "clickExtras") {
                        if (ret.index == 0) {
                            open_camera()
                        } else if (ret.index == 1) {
                            photo_album()
                        } else if (ret.index == 2) {
                            api.openWin({
                                name: "kaifang_win",
                                url: "widget://html/fangan/kaifang_win.html",
                                pageParam: {
                                    index: 3,
                                    info: {
                                        userId: api.pageParam.uid
                                    }
                                }
                            })
                        }
                    }
                });
            }
        });
    }

    //使用vue对数据进行绑定
    var app = new Vue({
        el: '#app',
        data: {
            identifier: "", // 该用户账号
            identifier_UserSig: "", //  用户账号对应的验签
            side_identifier: "", // 对方用户的账号
            chat_records: [],
            case_show: false,
            fangan: "",
            fangan_list: 0,
            page: 1,
            bingli_list: [], // 病历的数据
            user_info: "", // 用户信息
            doctor_avatar: IMAGE_HTTP + $api.getStorage("emp").avatar,
            user_avatar: "",
        },
        methods: {
            clickPlan: function (planId) {
                api.openWin({
                    name: "fangan_win",
                    url: "fangan_win.html",
                    pageParam: {
                        id: planId
                    }
                })
            },
            // 图片预览
            viewImgs: function (img_src) {
                // alert(JSON.stringify(imgs_url))
                var UIPhotoViewer = api.require('UIPhotoViewer');
                UIPhotoViewer.open({
                    images: [img_src],
                    placeholderImg: 'widget://res/logo.png',
                    bgColor: '#000'
                }, function (ret, err) {
                    if (ret) {
                        // alert(JSON.stringify(ret));
                        if (ret.eventType == 'click') {
                            UIPhotoViewer.close();
                        }
                    }
                });
            }
        },

    })


    //打开相册
    function photo_album() {
      var trs = isJuris('photos', '需要获取您的相册权限，才能上传照片，请问是否同意？'); //相机权限
      if(!trs){
        toast("获取相册权限失败")
        return;
      }
        api.getPicture({
            sourceType: 'library',
            encodingType: 'jpg',
            mediaValue: 'pic',
            targetWidth: '780',
            targetHeight: '1040',
            destinationType: 'url',
            quality: 70,
            saveToPhotoAlbum: false
        }, function (ret, err) {
            if (ret && ret.data) {
                //返回的本地路径
                // alert(JSON.stringify(ret))
                var returnUrl = ret.data;
                //上传图片
                api.ajax({
                    url: "https://api.zetikeji.com/api/storage/file",
                    method: 'post',
                    data: {
                        values: {
                            name: '聊天图'
                        },
                        files: {
                            file: returnUrl
                        }
                    }
                }, function (ret, err) {
                    if (ret.code == 200) {
                        //上传图片
                        var tencentIm = api.require('tencentIm');
                        tencentIm.sendImage({
                            peer: app.side_identifier,
                            type: 1,
                            imagePath: returnUrl,
                        }, function (res, err) {
                            if (res) {
                                // alert(JSON.stringify(res))
                                app.chat_records.length = 0
                                get_message()
                            }
                        });
                        // post_ajax("im/send-image", {
                        //     fromId: app.identifier,
                        //     toId: app.side_identifier,
                        //     content: "[图片]",
                        //     msgType: 2,
                        //     imagePath: ret.data,
                        //     imageUrl: IMAGE_HTTP
                        // }, function (result, error) {
                        //     if (result.code == 200) {

                        //     }
                        // });//endpost
                    }
                });


            }
        });
    }

    // 打开相机拍摄图片
    function open_camera() {
      var trs = isJuris('camera', '需要获取您的相机权限，才能上传照片，请问是否同意？'); //相机权限
      if(!trs){
        toast("获取相机权限失败")
        return;
      }
        var resultList = api.hasPermission({
            list: ['camera'],
        });
        api.requestPermission({
            list: ['camera'],
            code: 1
        }, function (ret, err) {
            if (ret) {
                api.getPicture({
                    sourceType: 'camera',
                    encodingType: 'jpg',
                    mediaValue: 'pic',
                    targetWidth: '780',
                    targetHeight: '1040',
                    destinationType: 'url',
                    quality: 70,
                    saveToPhotoAlbum: false
                }, function (ret, err) {
                    if (ret && ret.data) {
                        //返回的本地路径
                        var returnUrl = ret.data;
                        //上传图片
                        api.ajax({
                            url: "https://api.zetikeji.com/api/storage/file",
                            method: 'post',
                            data: {
                                values: {
                                    name: '聊天图'
                                },
                                files: {
                                    file: returnUrl
                                }
                            }
                        }, function (ret, err) {
                            if (ret.code == 200) {
                                //上传图片
                                var tencentIm = api.require('tencentIm');
                                tencentIm.sendImage({
                                    peer: app.side_identifier,
                                    type: 1,
                                    imagePath: returnUrl,
                                }, function (res, err) {
                                    if (res) {
                                        // alert(JSON.stringify(res))
                                        app.chat_records.length = 0
                                        get_message()
                                    } //else {
                                    //   alert(JSON.stringify(res))
                                    //     alert(JSON.stringify(err));
                                    // }
                                });
                                // post_ajax("im/send-image", {
                                //     fromId: app.identifier,
                                //     toId: app.side_identifier,
                                //     content: "[图片]",
                                //     msgType: 2,
                                //     imagePath: ret.data,
                                //     imageUrl: "https://oss.zetikeji.com/"
                                // }, function (result, error) {
                                //     if (result.code == 200) {

                                //     }
                                // });//endpost
                            }
                        });//endajax
                        // post_ajax("im/send-message", {
                        //     fromId: app.identifier,
                        //     toId: app.side_identifier,
                        //     content: "[图片]",
                        //     msgType: 1,
                        // }, function (result, error) {
                        //     if (result.code == 200) {
                        //         var tencentIm = api.require('tencentIm');
                        //         tencentIm.sendImage({
                        //             peer: app.side_identifier,
                        //             type: 1,
                        //             imagePath: returnUrl,
                        //         }, function (res, err) {
                        //             if (res) {
                        //                 // alert(JSON.stringify(res))
                        //                 app.chat_records.length = 0
                        //                 get_message()
                        //             }
                        //         });
                        //     }
                        // })//endpost
                    }
                });
            }
        })
    }


    // 获取五十条聊天数据
    function get_message() {
        var tencentIm = api.require('tencentIm');
        tencentIm.setRead({
            peer: app.side_identifier,
            type: 1,
        }, function (ret, err) {

        });
        tencentIm.getMessage({
            peer: app.side_identifier,
            count: 50,
            type: 1
        }, function (ret, err) {
            if (ret && typeof ret.messages != undefined) {
                //alert(JSON.stringify(ret));
                var messages = ret.messages;
                var item = null;
                for (var key in messages) {
                    for (elemKey in messages[key].elems) {
                        if (messages[key].elems[elemKey].type == 4) {
                            //图片消息
                            item = {
                                msgType: "image",
                                sender: messages[key].sender,
                                thumb: "",
                                origImage: "",
                                largeImage: "",
                                timestamp: messages[key].timestamp
                            };
                            for (var imageKey in messages[key].elems[elemKey].imageList) {
                                var imageList = messages[key].elems[elemKey].imageList
                                if (String(imageList[imageKey].type) == "Original") {
                                    item.origImage = imageList[imageKey].url;
                                    continue;
                                } else if (String(imageList[imageKey].type) == "Thumb") {
                                    item.thumb = imageList[imageKey].url;
                                    continue;
                                } else if (String(imageList[imageKey].type) == "Large") {
                                    item.largeImage = imageList[imageKey].url;
                                    continue;
                                }
                            }//endfor
                        } else if (messages[key].elems[elemKey].type == 1) {
                            //文本消息
                            item = {
                                msgType: "text",
                                text: "",
                                sender: messages[key].sender,
                                timestamp: messages[key].timestamp
                            }
                            var text = String(messages[key].elems[elemKey].text);
                            if (text.match(/\{(.+?)\}/g)) {
                                item.msgType = "json";
                                item.text = JSON.parse(text.match(/\{(.+?)\}/g));
                            } else {
                                item.msgType = "text";
                                item.text = text;
                            }//endif
                        }//endif
                    }//endfor
                    app.chat_records.unshift(item)
                }//endfor
                app.$nextTick(function () {
                    hb_init_openwinby_click();
                })
                setTimeout(function () {
                    $('html, body').animate({
                        scrollTop: $('#maodian_div').offset().top
                    }, 300)
                }, 200);

                // alert(JSON.stringify(app.chat_records))
            } else {
                tencentIm.getLocalMessage({
                    peer: app.side_identifier,
                    count: 50,
                }, function (ret, err) {
                    if (ret && typeof ret.messages != undefined) {
                        // alert(JSON.stringify(ret));
                        var messages = ret.messages;
                        var item = null;
                        for (var key in messages) {
                            for (elemKey in messages[key].elems) {
                                if (messages[key].elems[elemKey].type == 4) {
                                    //图片消息
                                    item = {
                                        msgType: "image",
                                        sender: messages[key].sender,
                                        thumb: "",
                                        origImage: "",
                                        largeImage: "",
                                        timestamp: messages[key].timestamp
                                    };
                                    for (var imageKey in messages[key].elems[elemKey].imageList) {
                                        var imageList = messages[key].elems[elemKey].imageList
                                        if (String(imageList[imageKey].type) == "Original") {
                                            item.origImage = imageList[imageKey].url;
                                            continue;
                                        } else if (String(imageList[imageKey].type) == "Thumb") {
                                            item.thumb = imageList[imageKey].url;
                                            continue;
                                        } else if (String(imageList[imageKey].type) == "Large") {
                                            item.largeImage = imageList[imageKey].url;
                                            continue;
                                        }
                                    }//endfor
                                } else if (messages[key].elems[elemKey].type == 1) {
                                    //文本消息
                                    item = {
                                        msgType: "text",
                                        text: "",
                                        sender: messages[key].sender,
                                        timestamp: messages[key].timestamp
                                    }
                                    var text = String(messages[key].elems[elemKey].text);
                                    if (text.match(/\{(.+?)\}/g)) {
                                        item.msgType = "json";
                                        item.text = JSON.parse(text.match(/\{(.+?)\}/g));
                                    } else {
                                        item.msgType = "text";
                                        item.text = text;
                                    }//endif
                                }//endif
                            }//endfor
                            app.chat_records.unshift(item)
                        }//endfor
                        // alert(JSON.stringify(app.chat_records))

                        app.$nextTick(function () {
                            hb_init_openwinby_click();
                        })
                        setTimeout(function () {
                            $('html, body').animate({
                                scrollTop: $('#maodian_div').offset().top
                            }, 300)
                        }, 1000);
                    } //else {
                    //     alert("网络不通畅，请稍后再试！");
                    // }
                });
            }
        });
    }

    function get_new_msg() {
        var tencentIm = api.require('tencentIm');
        tencentIm.addNewMessageListener({}, function (ret, err) {
            if (ret.status) {
                //alert(JSON.stringify(ret));
                var messages = ret.messages;
                var item = null;
                for (var key in messages) {
                    for (elemKey in messages[key].elems) {
                        if (messages[key].elems[elemKey].type == 4) {
                            //图片消息
                            item = {
                                msgType: "image",
                                sender: messages[key].sender,
                                thumb: "",
                                origImage: "",
                                largeImage: "",
                                timestamp: messages[key].timestamp
                            };
                            for (var imageKey in messages[key].elems[elemKey].imageList) {
                                var imageList = messages[key].elems[elemKey].imageList
                                if (String(imageList[imageKey].type) == "Original") {
                                    item.origImage = imageList[imageKey].url;
                                    continue;
                                } else if (String(imageList[imageKey].type) == "Thumb") {
                                    item.thumb = imageList[imageKey].url;
                                    continue;
                                } else if (String(imageList[imageKey].type) == "Large") {
                                    item.largeImage = imageList[imageKey].url;
                                    continue;
                                }
                            }//endfor
                        } else if (messages[key].elems[elemKey].type == 1) {
                            //文本消息
                            item = {
                                msgType: "text",
                                text: "",
                                sender: messages[key].sender,
                                timestamp: messages[key].timestamp
                            }
                            var text = String(messages[key].elems[elemKey].text);
                            if (text.match(/\{(.+?)\}/g)) {
                                item.msgType = "json";
                                item.text = JSON.parse(text.match(/\{(.+?)\}/g));
                            } else {
                                item.msgType = "text";
                                item.text = text;
                            }//endif
                        }//endif
                    }//endfor
                    app.chat_records.push(item)
                }//endfor

                app.$nextTick(function () {
                    hb_init_openwinby_click();
                })
                $('html, body').animate({
                    scrollTop: $('#maodian_div').offset().top
                }, 300)
            }
        });
    }
</script>
